package cn.eeepay.framework.service.impl;

import cn.eeepay.framework.dao.AccountAddrOrderDao;
import cn.eeepay.framework.dao.JumpRouteQuotaLogDao;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.AccountAddrOrder;
import cn.eeepay.framework.model.JumpRouteConfig;
import cn.eeepay.framework.model.JumpRouteQuotaLog;
import cn.eeepay.framework.service.JumpRouteQuotaLogService;
import cn.eeepay.framework.service.JumpRouteService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class JumpRouteQuotaLogServiceImpl implements JumpRouteQuotaLogService {

    @Autowired
    AccountAddrOrderDao accountAddrOrderDao;
    @Autowired
    JumpRouteService jumpRouteService;
    @Autowired
    JumpRouteQuotaLogDao jumpRouteQuotaLogDao;

    @Transactional
    @Override
    public void moveLog() {
        String yesterDay = DateUtil.yesterday().toString(new SimpleDateFormat("yyyy-MM-dd"));
        List<AccountAddrOrder> accountAddrOrders = accountAddrOrderDao.collectOrderByJumpIdAndGroupCode(yesterDay);
        if(CollectionUtil.isNotEmpty(accountAddrOrders)){
            for (AccountAddrOrder accountAddrOrder : accountAddrOrders) {
                Integer jumpId = accountAddrOrder.getJumpId();
                JumpRouteConfig jumpRouteConfig = jumpRouteService.getById(jumpId);
                if(jumpRouteConfig != null){
                    String remark = jumpRouteConfig.getRemark();
                    BigDecimal targetAmount = jumpRouteConfig.getTargetAmount();
                    Integer groupCode = jumpRouteConfig.getGroupCode();

                    JumpRouteQuotaLog jumpRouteQuotaLog = new JumpRouteQuotaLog();
                    jumpRouteQuotaLog.setJumpId(jumpId);
                    jumpRouteQuotaLog.setRemark(remark);
                    jumpRouteQuotaLog.setTargetQuota(targetAmount);
                    String code = groupCode != null ? groupCode.toString() : null;
                    jumpRouteQuotaLog.setGroupCode(code);
                    jumpRouteQuotaLog.setAccountDate(DateUtil.yesterday());
                    jumpRouteQuotaLog.setCreateTime(new Date());

                    //要根据日期和jumpid查，有则更新，无则新增
                    jumpRouteQuotaLogDao.insert(jumpRouteQuotaLog);
                }
            }
        }
    }


    @Override
    public List<JumpRouteQuotaLog> routeMonitorLogPage(Integer jumpId, String today, Page<JumpRouteQuotaLog> page) {
        return jumpRouteQuotaLogDao.routeMonitorLogPage(jumpId,today,page);
    }

    @Override
    public List<JumpRouteQuotaLog> routeMonitorHistoryLogPage(Integer jumpId, String dateStart, String dateEnd, Page<JumpRouteQuotaLog> page) {
        return jumpRouteQuotaLogDao.routeMonitorHistoryLogPage(jumpId,dateStart,dateEnd,page);
    }
}
